/**
 * Created by taohj on 2014/5/21.
 */

var dictionary = new Object();


/**
 * 模块列表
 * */
dictionary.initgrid = function (groupId) {
    dictionary.datagrid = $("#sys_dictionary_list_datagrid");
    dictionary.datagrid.datagrid({
        url: functions.get("SysDictionaryList")+"?groupId="+groupId,
        singleSelect: true,
        collapsible: true,
        pagination: true,
        rownumbers: true,
        fitColumns:true,
        sortName:"id",
        sortOrder:"asc",
        fit: true,
        pageList: [20, 50, 100],
        pageSize: 20,
        onDblClickRow:function(rowIndex, rowData){

            var options=dictionary.datagrid.datagrid("options");
            options.toolbar[2].handler();
        },
        toolbar: [
            {
                iconCls: 'icon-add',
                text: 'Create',
                handler: function () {
                    var createDialog = $("<div></div>");
                    createDialog.dialog({
                        title: 'SysDictionaryCreate',
                        href: functions.get('SysDictionaryCreateView')+'?groupId='+groupId,
                        modal: true,
                        width: 300
                    });

                }
            },
            '-',
            {
                iconCls: 'icon-edit',
                text: 'Modify',
                handler: function () {
                    var selectRow= dictionary.datagrid.datagrid("getSelected");
                    if(!selectRow){
                        //todo
                        return;
                    }

                    var id="";
                    if(selectRow){
                        id=selectRow.id;
                    }

                    var createDialog = $("<div></div>");
                    createDialog.dialog({
                        title: 'SysDictionaryModify',
                        href:functions.get('SysDictionaryModifyView')+'?id='+id,
                        modal: true,
                        width: 300
                    });

                }
            },
            '-',
            {
                iconCls: 'icon-remove',
                text: 'Remove',
                handler: function () {

                    var selectRow= dictionary.datagrid.datagrid("getSelected");
                    if(!selectRow){
                        //todo
                        return;
                    }

                    $.messager.confirm('Confirm', 'Are you sure remove selected object?', function(r){
                        if (r){
                            $.post(functions.get('SysDictionaryRemove'), {
                                id:selectRow.id
                            }, function (result) {
                                util.requestUtil(result,null,dictionary.datagrid);
                            });
                        }
                    });
                    console.info(selectRow);
                }
            }
        ],
        columns:[[
            {field:"id",title:"ID",width:60,sortable:true},
            {field:"name",title:"NAME",width:200,sortable:true},
            {field:"value",title:"VALUE",width:200,sortable:true},
            {field:"group",title:"Group",width:200,sortable:true,formatter:function(value){
                return value?value.name:"";
            }},
            {field:"description",title:"DESCRIPTION"}
        ]]
    });
};


//新建提交
dictionary.createSubmit = function (button) {
    var form =util.form(button);
    var params = util.serializeObject(form);
    if (util.form(button).form("validate")) {
        $.post(functions.get('SysDictionaryCreate'), params, function (result) {
            var dialog = $(button).closest("div.window-body");
            util.requestUtil(result,dialog,dictionary.datagrid)
        });
    }
};


//修改提交
dictionary.modifySubmit = function (button) {
    var form =util.form(button);
    var params = util.serializeObject(form);
    if (util.form(button).form("validate")) {
        $.post(functions.get('SysDictionaryModify'), params, function (result) {
            var dialog = $(button).closest("div.window-body");
            util.requestUtil(result,dialog,dictionary.datagrid);
        });
    }
};


//搜索
dictionary.searchSubmit=function(button){
    var form =util.form(button);
    var params = util.serializeObject(form);
    dictionary.datagrid.datagrid("load",params);
}







